#!/usr/bin/env python3
import os
import random
import sys
import argparse
from string import ascii_letters  
from termcolor import cprint


colors = {
    'mail' : 'blue',    
    'normal' :'green'
}

mails_type = {
    'qq': '@qq.com',
    '163': '@163.com',
    '126': '@126.com',
    'yeah': '@yeah.net'
}




le = ascii_letters + '0123456789'
root_dir = os.path.join(os.environ['HOME'], ".U-account")

def args():
    doc = """
     this is script for gen account
    --- write by qingluan
    """
    parser = argparse.ArgumentParser(usage=" how to use this "
        , description=doc)
    parser.add_argument("-t","--type", default="mail", help="type of generate")
    parser.add_argument("-l","--lst", default=False, action='store_true')
    parser.add_argument("-mt","--mail-type", default='126', help="mail type")
    parser.add_argument("--args", default='', help="args for options")
    return parser.parse_args()

def gen_serise(max_len=7):
    count = 0
    u = ''
    while count < max_len + random.randint(3,7):
        c = os.urandom(1).decode('utf8','ignore').strip()
        try:
            ch = ord(c)
        except:
            continue
        if  (ch >= 48 and ch <= 57) or (ch >= 65 and ch <= 90) or (ch >= 97 and ch <= 122):
            # print(c)
            u += c
            count += 1
    return u

def to_save(u,p,ty):
    if not os.path.exists(os.path.join(root_dir,ty)):
        with open(os.path.join(root_dir,ty), "w") as fp:
            pass

    with open(os.path.join(root_dir,ty), "a+") as fp:
        print(u, p, file=fp)

def got():
    for file in os.listdir(root_dir):
        for line in open(os.path.join(root_dir,file)):
            cprint(line, colors[file])

if __name__ == "__main__":
    ar = args()
    if ar.lst:
        got()
        sys.exit(0)

    if ar.type:
        a = ar.args if ar.args else None
        u = None
        if a:
            u = gen_serise(a)
        else :
            u = gen_serise()
        u += mails_type[ar.mail_type]
        p = gen_serise(8)
        to_save(u, p, ar.type)


